package com.qdxwx.manage.controller.order;

import com.qdxwx.common.json.AjaxResult;
import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.page.TableDataInfo;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.common.utils.TKString;
import com.qdxwx.data.service.order.OrderExpressService;
import com.qdxwx.data.service.order.OrderService;
import com.qdxwx.manage.controller.BaseController;
import com.qdxwx.models.order.Order;
import com.qdxwx.models.order.OrderExpress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Controller
@RequestMapping("/order/express")
public class OrderExpressController extends BaseController {

    @Autowired
    private OrderExpressService expressService;

    @Autowired
    private OrderService orderService;

    /**
     * 列表首页
     */
    @GetMapping("/list.html")
    public String List() {
        return "order/order-express-list";
    }

    /**
     * 查询订单列表json
     */
    @RequestMapping(value = "/list.json")
    @ResponseBody
    public TableDataInfo list(HttpServletRequest request) {
        String searchKey = request.getParameter("searchKey");
        int pageLimit = TKConvert.toInteger(request.getParameter("limit"), 10);
        int page = TKConvert.toInteger(request.getParameter("page"), 1);
        OrderExpress express = new OrderExpress();
        express.setStatus(TKConvert.toInteger(request.getParameter("status"),null));
        if (searchKey != null && searchKey.length() > 0) {
            if (TKConvert.toInteger(searchKey) > 0) {
                express = expressService.get(TKConvert.toInteger(searchKey));
                if (express == null) {
                    return TableDataInfo.success(null, 0);
                }
                List<OrderExpress> expressList = new ArrayList<>();
                expressList.add(express);
                return TableDataInfo.success(expressList, 1);
            }else if (TKString.isMobile(searchKey)) {
                express.setMobile(searchKey);
            } else {
                express.setOrderNo(searchKey);
            }
        }
        long count = expressService.count(express);
        PageRequest paginator = new PageRequest(page, pageLimit);
        List<OrderExpress> list = expressService.getList(express, paginator);
        return TableDataInfo.success(list, count);
    }

    @GetMapping("/edit.html")
    public String edit(HttpServletRequest request, ModelMap mmap) {
        int id = TKConvert.toInteger(request.getParameter("id"));
        if(id < 1) return error404;
        OrderExpress express = expressService.get(id);
        if(express == null) return error404;
        mmap.put("express", express);
        return "order/order-express-edit";
    }

    @PostMapping("/edit.json")
    @ResponseBody
    public AjaxResult edit(HttpServletRequest request) {
        int id = TKConvert.toInteger(request.getParameter("id"));
        if (id < 1) return AjaxResult.error("缺少id参数");
        OrderExpress express = expressService.get(id);
        if (express == null) return AjaxResult.error("找不到指定数据");
        String address = request.getParameter("address");
        if (TKString.isEmpty(address)) return AjaxResult.error("配送地址不能为空");
        String mobile = request.getParameter("mobile");
        if (!TKString.isMobile(mobile)) return AjaxResult.error("请输入正确的手机号码");
        String userName = request.getParameter("userName");
        if (TKString.isEmpty(userName)) return AjaxResult.error("联系人不能为空");
        String courierNo = request.getParameter("courierNo");
        int status = TKConvert.toInteger(request.getParameter("status"));
        express.setAddress(address);
        express.setMobile(mobile);
        express.setUserName(userName);
        express.setCourierNo(courierNo);
        express.setStatus(status == 0 ? status : 1);
        int row = expressService.update(express);
        return row > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    @GetMapping("/view.html")
    public String view(HttpServletRequest request, ModelMap mmap) {
        int id = TKConvert.toInteger(request.getParameter("id"));
        if(id < 1) return error404;
        OrderExpress express = expressService.get(id);
        Order order = orderService.getByOrderNo(express.getOrderNo());
        if(express == null || order == null) return error404;
        mmap.put("order", order);
        mmap.put("express", express);
        return "order/order-express-view";
    }

}
